#!/bin/bash

source build/hooks/version.sh.inc # $VERSION_STRING

BUILD_DIR=build
PKG_DIR=$BUILD_DIR/packaging
VER=${VERSION_STRING//v}


echo "running pre-commit hooks"

# echo "minor_version_tag=$minor_version_tag"
# echo "n_revisions_ahead=$n_revisions_ahead"
echo "upgrading version to '$VERSION_STRING'"
sed -i -e "s/^VERSION =.*$/VERSION = $VERSION_STRING/"           $BUILD_DIR/Makefile
git add                                                          $BUILD_DIR/Makefile
sed -i -e "s/\/v.*\.tar\.gz$/\/$VERSION_STRING\.tar\.gz/"        $PKG_DIR/_service
git add                                                          $PKG_DIR/_service
sed -i -e "s/^pkgver=.*$/pkgver=$VER/"                           $PKG_DIR/PKGBUILD
sed -i -e "s/^validpgpkeys=.*$/validpgpkeys=('$GPG_KEY')/"       $PKG_DIR/PKGBUILD
git add                                                          $PKG_DIR/PKGBUILD
sed -i -e "s/^Version:.*$/Version:       $VER/"                  $PKG_DIR/loopidity.spec
tail -n 2 $PKG_DIR/loopidity.spec | wc -c | xargs -I {} truncate $PKG_DIR/loopidity.spec -s -{} ;
(($(grep -c "pre-commit" $PKG_DIR/loopidity.spec) - 1)) && echo "$PKG_DIR/loopidity.spec malformed" && exit 1
(($(grep -c "%changelog" $PKG_DIR/loopidity.spec) - 1)) && echo "$PKG_DIR/loopidity.spec malformed" && exit 1
echo "* $(date '+%a %b %d %Y') $(git config user.name)"       >> $PKG_DIR/loopidity.spec
echo "- $VERSION_STRING"                                      >> $PKG_DIR/loopidity.spec
git add                                                          $PKG_DIR/loopidity.spec
sed -i -e "s/^Version:.*$/Version:           $VER-1/"            $PKG_DIR/loopidity.dsc
git add                                                          $PKG_DIR/loopidity.dsc
